Nowadays comunication between applications is an active topic with daily usage and a large amount of pratical appliances. While developing an app in witch I had to consume an OData I found out that combining Linq with my code made this operation pretty easy.
The algorithm to consume OData starts with adding a service reference to Visual Studio:
After adding the service reference in wich you define the uri to the service, we start building our code.
In your code the algorithm is the following:
- Define the Uri to your OData Service
- Define the context of your odata, wich contains all entities exposed by the service.
- Query the context using Linq
- Print the result
Easy and simple.
Example code:
01 | public static void Main( string [] args){ |
04 | ODataService.ServiceEntities context = new ODataService.ServiceEntities (serviceUri); |
06 | context.Credentials = new System.Net.NetworkCredential(Username,Password); |
08 | var query = from ServiceObject in context.YourEntity |
09 | select ServiceObject ; |
11 | foreach (var myObject in query) |
13 | Console.WriteLine( "\n Field1: {0} | Field2: {1}" , |
14 | myObject .Field1, myObject .Field2); |
That’s it.
Thank you,
Rui Machado
rpmachado.wordpress.com